<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>购物车布局</title>
  <link href="css/cart.css" rel="stylesheet">
</head>

<body>

  <div id="app" class="container">

    <!-- 空购物车界面 -->
    <div class="empty-cart-message">
      <div class="empty-cart-image"></div>
      您的购物车空空如也
      <a href="#" class="go-checkout">去购物</a>
    </div>


    <div class="cart">
      <h1>科泰淘购物车</h1>
      <div class="clearfix">
        <a href="#" class="delete-selected" @click="del()">删除选中</a>
        <a href="#" class="clear-cart" @click="delAll()">清空购物车</a>
      </div>
      <div class="cart-item" v-for="(lists,index) in cartList">
        <div class="cart-item-details">
          <input type="checkbox" class="cart-item-checkbox" @click="choose(index)" :checked="lists.checked">
          <img :src="lists.imgUrl" alt="男士休闲裤夏季新款">
          <div class="cart-item-name-sku">
            <div class="cart-item-name">{{lists.goodsName}}</div>
            <div class="cart-item-sku">颜色：{{lists.color}} 尺码：{{lists.size}}</div>
          </div>
        </div>
        <div class="cart-item-quantity-price">
          <div class="cart-item-quantity">
            <button class="quantity-btn minus-btn" @click="subtrahend(index)">-</button>
            <input type="text" name="quantity" class="quantity" v-model="lists.count" @blur="blurs(index)">
            <button class="quantity-btn plus-btn" @click="addend(index)">+</button>
          </div>
          <div class="cart-item-price">￥{{lists.price.toFixed(2)}}</div>
        </div>
      </div>
      <!-- <div class="cart-item">
        <div class="cart-item-details">
          <input type="checkbox" class="cart-item-checkbox">
          <img src="images/goods-2.png" alt="男士夏季装短袖polo衫">
          <div class="cart-item-name-sku">
            <div class="cart-item-name">男士夏季装短袖polo衫</div>
            <div class="cart-item-sku">颜色：浅蓝 尺码：M</div>
          </div>
        </div>
        <div class="cart-item-quantity-price">
          <div class="cart-item-quantity">
            <button class="quantity-btn minus-btn" @click="">-</button>
            <input type="text" name="quantity" class="quantity" value="1">
            <button class="quantity-btn plus-btn" @click="">+</button>
          </div>
          <div class="cart-item-price">￥199.00</div>
        </div>
      </div> -->

      <div class="total-price-container">
        <div class="total-count">已选 <span class="count-value">{{selected}}</span> 件</div>
        <div class="total-price">总计：￥{{totalAmount.toFixed(2)}}</div>
      </div>

      <div class="clearfix">
        <div class="select-all-container">
          <input type="checkbox" id="select-all-checkbox" class="cart-item-checkbox" @click="selectAll()" >
          <label for="select-all-checkbox">全选</label>
        </div>
        <a href="#" class="go-checkout">去结算</a>
      </div>
    </div>

  </div>

  <script src="js/vue.js"></script>
  <script src="js/cart-vue.js"></script>
  <script>
    new Vue({
      el:'#app',
      data:{
        totalAmount:0.00,
        selected:0,
        check:false,
        cartList: [
          {
            id: 1,
            goodsName: '男士休闲裤夏季新款',
            count: 1,
            price: 99.0,
            color: '灰色',
            size: 'L',
            imgUrl: 'images/goods-1.png',
            checked: false,
          },
          {
            id: 2,
            goodsName: '男士夏季装短袖polo衫',
            count: 1,
            price: 199.0,
            color: '浅蓝',
            size: 'M',
            imgUrl: 'images/goods-2.png',
            checked: false,
          },
        ],
      },
      methods:{
        selectAll(){
          for(let i = 0;i < this.cartList.length;i++){
            if(!this.cartList[i].checked){
              this.cartList[i].checked = true;
              this.totalAmount = this.totalAmount + (this.cartList[i].price * this.cartList[i].count);
              this.selected = this.selected + 1;
            } else {
              this.cartList[i].checked = false;
              this.totalAmount = this.totalAmount - (this.cartList[i].price * this.cartList[i].count);
              this.selected = this.selected - 1;
            }
          }
        },
        addend(index){
          if(!this.cartList[index].checked){
            this.cartList[index].count = this.cartList[index].count + 1;
          } else {
            this.cartList[index].count = this.cartList[index].count + 1;
            this.totalAmount = this.totalAmount + this.cartList[index].price;
          }
        },
        subtrahend(index){
          if(this.cartList[index].count > 1){
            if(!this.cartList[index].checked){
              this.cartList[index].count = this.cartList[index].count - 1;
            } else {
              this.cartList[index].count = this.cartList[index].count - 1;
              this.totalAmount = this.totalAmount - this.cartList[index].price;
            }
          } else {
            alert("商品数量不能小于1")
          }
        },
        blurs(index){
          if(this.cartList[index].count == null || this.cartList[index].count == ''){
            this.cartList[index].count = 1;
          }
        },
        choose(index){
          if(!this.cartList[index].checked){
            this.cartList[index].checked = true;
            this.totalAmount = this.totalAmount + (this.cartList[index].price * this.cartList[index].count);
            this.selected = this.selected + 1;
          } else {
            this.cartList[index].checked = false;
            this.totalAmount = this.totalAmount - (this.cartList[index].price * this.cartList[index].count);
            this.selected = this.selected - 1;
          }
        },
        del(){
          this.cartList = this.cartList.filter(cart => !cart.checked);
          this.selected = 0;
          this.totalAmount = 0;
        },
        delAll(){
          this.cartList = this.cartList.filter(cart => cart.checked);
          this.cartList = this.cartList.filter(cart => !cart.checked);
          this.selected = 0;
          this.totalAmount = 0;
        }
      }
    })
  </script>
</body>

</html>